package com.pig4cloud.pig.module.controller.farm;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.pig.common.mybatis.page.PageUtils;
import com.pig4cloud.pig.common.mybatis.page.TableDataInfo;
import com.pig4cloud.pig.module.api.entity.farm.FarmAdopt;
import com.pig4cloud.pig.module.api.entity.farm.FarmAdoptGiven;
import com.pig4cloud.pig.module.api.entity.farm.FarmAdoptImg;
import com.pig4cloud.pig.module.service.farm.IFarmAdoptGivenService;
import com.pig4cloud.pig.module.service.farm.IFarmAdoptImgService;
import com.pig4cloud.pig.module.service.farm.IFarmAdoptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 领养后的赠品内容Controller
 *
 * @author ruoyi
 * @date 2022-10-21
 */
@Validated
@Api(value = "领养后的赠品内容控制器", tags = {"领养后的赠品内容管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/given")
public class FarmAdoptGivenController {

    private final IFarmAdoptGivenService farmAdoptGivenService;
    private final IFarmAdoptService farmAdoptService;
    private final IFarmAdoptImgService farmAdoptImgService;
    @Value("${ruoyi.imgUrl}")
    private String imgUrl;
    /**
     * 查询领养后的赠品内容列表
     */
    @ApiOperation("查询领养后的赠品内容列表")
    @PreAuthorize("@pms.hasPermission('module:given:list')")
    @GetMapping("/list")
    public TableDataInfo<FarmAdoptGiven> list(@Validated FarmAdoptGiven farmAdoptGiven) {
        QueryWrapper<FarmAdoptGiven> qw = new QueryWrapper<>();
        if(farmAdoptGiven.getAdoptId()!=null){
            qw.eq("adopt_id",farmAdoptGiven.getAdoptId());
        }
        TableDataInfo<FarmAdoptGiven> td = PageUtils.buildDataInfo(farmAdoptGivenService.page(PageUtils.buildPage(),qw));
        List<FarmAdoptGiven> list = td.getData();
        for(FarmAdoptGiven list1:list){
            FarmAdopt farmAdopt=farmAdoptService.findById(list1.getAdoptId());
            list1.setAdoptName(farmAdopt.getAdoptName());
        }
        return td;
    }

    /**
     * 导出领养后的赠品内容列表
     */
    @ApiOperation("导出领养后的赠品内容列表")
    @PreAuthorize("@pms.hasPermission('module:given:export')")
    @SysLog("领养后的赠品内容")
    @GetMapping("/export")
    public List export(@Validated FarmAdoptGiven farmAdoptGiven) {
        List<FarmAdoptGiven> list = farmAdoptGivenService.list();
        // ExcelUtil<FarmAdoptGiven> util = new // ExcelUtil<FarmAdoptGiven>(FarmAdoptGiven.class);
        return  list;// util.exportExcel(list, "领养后的赠品内容");
    }

    /**
     * 获取领养后的赠品内容详细信息
     */
    @ApiOperation("获取领养后的赠品内容详细信息")
    @PreAuthorize("@pms.hasPermission('module:given:query')")
    @GetMapping("/{id}")
    public R<FarmAdoptGiven> getInfo(@NotNull(message = "主键不能为空") @PathVariable("id") Long id) {
        FarmAdoptGiven farmAdoptGiven = farmAdoptGivenService.getById(id);
        String lunboImgs = "";
        QueryWrapper<FarmAdoptImg> qw = new QueryWrapper<>();
        qw.eq("adopt_id",id);
        qw.eq("file_type","jpg");
        List<FarmAdoptImg> lunbos = farmAdoptImgService.list(qw);
        for (FarmAdoptImg img : lunbos){
            if(StrUtil.isNotEmpty(img.getImgUrl())) {
                lunboImgs += imgUrl + img.getImgUrl() + ",";
                img.setImgUrl(imgUrl + img.getImgUrl());
            }
        }
        farmAdoptGiven.setAdoptImgs(lunbos);
        if(StrUtil.isNotEmpty(lunboImgs)){
            lunboImgs = lunboImgs.substring(0,lunboImgs.length() - 1);
        }
        farmAdoptGiven.setAdoptImg(lunboImgs);
        return R.ok(farmAdoptGiven);
    }

    /**
     * 新增领养后的赠品内容
     */
    @ApiOperation("新增领养后的赠品内容")
    @PreAuthorize("@pms.hasPermission('module:given:add')")
    @SysLog("领养后的赠品内容")

    @PostMapping()
    public R<Void> add(@Valid @RequestBody FarmAdoptGiven farmAdoptGiven) {
        boolean bl = farmAdoptGivenService.save(farmAdoptGiven);
        if(bl){
            List<FarmAdoptImg> delImg = new ArrayList<>();

            List<FarmAdoptImg> imgs = farmAdoptGiven.getAdoptImgs();

            QueryWrapper<FarmAdoptImg> qw = new QueryWrapper<>();
            qw.eq("adopt_id",farmAdoptGiven.getAdoptId());
            /*qw.eq("file_type","jpg");*/
            List<FarmAdoptImg> infoImgs = farmAdoptImgService.list(qw);
            if(CollUtil.isNotEmpty(infoImgs)){
                for (FarmAdoptImg img : infoImgs){
                    int isHas = 0;
                    for (FarmAdoptImg img1 : imgs){
                        if(img1.getId()!=null && img.getImgUrl().equals(img1.getImgUrl())){//存在
                            isHas = 1;
                        }
                    }

                    if(isHas == 0){
                        delImg.add(img);
                    }
                }
            }
            if(CollUtil.isNotEmpty(imgs)){
                for (FarmAdoptImg img : imgs){
                    if(img.getId() == null){
                        img.setAdoptId(farmAdoptGiven.getAdoptId());
                        img.setSort(1L);
                        img.setFileType("jpg");
                        farmAdoptImgService.save(img);
                    }
                }
            }
            if(CollUtil.isNotEmpty(delImg)){
                for (FarmAdoptImg img : delImg){
                    farmAdoptImgService.removeById(img.getId());
                }
            }
        }
        return R.okByRow(bl? 1 : 0);
    }

    /**
     * 修改领养后的赠品内容
     */
    @ApiOperation("修改领养后的赠品内容")
    @PreAuthorize("@pms.hasPermission('module:given:edit')")
    @SysLog("领养后的赠品内容")

    @PutMapping()
    public R<Void> edit(@Valid @RequestBody FarmAdoptGiven farmAdoptGiven) {
        if(farmAdoptGiven.getImgUrl()==null||farmAdoptGiven.getImgUrl()==""){
            farmAdoptGiven.setImgUrl("0");
        }

        boolean bl = farmAdoptGivenService.updateById(farmAdoptGiven);
        if(bl){
            List<FarmAdoptImg> delImg = new ArrayList<>();

            List<FarmAdoptImg> imgs = farmAdoptGiven.getAdoptImgs();

            QueryWrapper<FarmAdoptImg> qw = new QueryWrapper<>();
            qw.eq("adopt_id",farmAdoptGiven.getAdoptId());
            /*qw.eq("file_type","jpg");*/
            List<FarmAdoptImg> infoImgs = farmAdoptImgService.list(qw);
            if(CollUtil.isNotEmpty(infoImgs)){
                for (FarmAdoptImg img : infoImgs){
                    int isHas = 0;
                    for (FarmAdoptImg img1 : imgs){
                        if(img1.getId()!=null && img.getImgUrl().equals(img1.getImgUrl())){//存在
                            isHas = 1;
                        }
                    }

                    if(isHas == 0){
                        delImg.add(img);
                    }
                }
            }
            if(CollUtil.isNotEmpty(imgs)){
                for (FarmAdoptImg img : imgs){
                    if(img.getId() == null){
                        img.setAdoptId(farmAdoptGiven.getAdoptId());
                        img.setSort(1L);
                        img.setFileType("jpg");
                        farmAdoptImgService.save(img);
                    }
                }
            }
            if(CollUtil.isNotEmpty(delImg)){
                for (FarmAdoptImg img : delImg){
                    farmAdoptImgService.removeById(img.getId());
                }
            }
        }
        return R.okByRow(bl? 1 : 0);
    }

    /**
     * 删除领养后的赠品内容
     */
    @ApiOperation("删除领养后的赠品内容")
    @PreAuthorize("@pms.hasPermission('module:given:remove')")
    @SysLog("领养后的赠品内容")
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable Long[] ids) {
        return R.okByRow(farmAdoptGivenService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
